package com.javalong.affairs.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.utils.StringUtils;

public class TopTitle extends RelativeLayout implements OnClickListener {

	public ImageView getIv_right_drawable() {
		return iv_right_drawable;
	}

	private ImageView iv_left_drawable;
	private ImageView iv_right_drawable;
	private TextView tv_title;
	private TextView tv_right_text;
	private LayoutInflater inflater = null;
	private boolean rightDrawable;

	public TopTitle(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public TopTitle(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		TypedArray typeArray = context.obtainStyledAttributes(attrs,
				R.styleable.TopTitle);
		int N = typeArray.getIndexCount();
		for (int i = 0; i < N; i++) {
			int attr = typeArray.getIndex(i);
			int resourceId = 0;
			switch (attr) {
			case R.styleable.TopTitle_leftDrawable:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_leftDrawable, 0);
				iv_left_drawable.setImageResource(resourceId > 0 ? resourceId
						: R.drawable.ic_launcher);
				break;
			case R.styleable.TopTitle_rightText:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_rightText, 0);
				String rightText = "";
				if (resourceId == 0) {
					rightText = typeArray
							.getString(R.styleable.TopTitle_rightText);
				} else {
					rightText = typeArray.getResources().getText(resourceId)
							.toString();
				}
				if (!StringUtils.isEmpty(rightText)) {
					tv_right_text.setText(rightText);
					tv_right_text.setVisibility(View.VISIBLE);
				}
				break;
			case R.styleable.TopTitle_titleText:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_titleText, 0);
				String titleText = "";
				if (resourceId == 0) {
					titleText = typeArray
							.getString(R.styleable.TopTitle_titleText);
				} else {
					titleText = typeArray.getResources().getText(resourceId)
							.toString();
				}
				if (!StringUtils.isEmpty(titleText)) {
					tv_title.setText(titleText);
					tv_title.setVisibility(View.VISIBLE);
				}
				break;
			case R.styleable.TopTitle_rightDrawable:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_rightDrawable, 0);
				if (resourceId != 0) {
					iv_right_drawable.setImageResource(resourceId);
					iv_right_drawable.setVisibility(View.VISIBLE);
				}
				break;
			case R.styleable.TopTitle_leftDrawableSize:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_leftDrawableSize, 0);
				float dime = 0;
				if (resourceId != 0) {
					dime = typeArray.getResources().getDimension(resourceId);
				} else {
					dime = typeArray.getDimension(
							R.styleable.TopTitle_leftDrawableSize, 0);
				}
				iv_left_drawable.getLayoutParams().width = (int) dime;
				iv_left_drawable.getLayoutParams().height = (int) dime;
				break;
			case R.styleable.TopTitle_rightTextSize:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_rightTextSize, 0);
				float textSize = 0;
				if (resourceId != 0) {
					textSize = typeArray.getResources()
							.getDimension(resourceId);
				} else {
					textSize = typeArray.getDimension(
							R.styleable.TopTitle_rightTextSize, 0);
				}
				tv_right_text.setTextSize(textSize);
				break;
			case R.styleable.TopTitle_rightTextColor:
				resourceId = typeArray.getResourceId(
						R.styleable.TopTitle_rightTextColor, 0);
				int color;
				if (resourceId != 0) {
					color = typeArray.getResources().getColor(resourceId);
				} else {
					color = typeArray.getColor(
							R.styleable.TopTitle_rightTextColor, 0);
				}
				tv_right_text.setTextColor(color);
				break;
			case R.styleable.TopTitle_rightDrawableVisible:
				boolean visible = typeArray.getBoolean(
						R.styleable.TopTitle_rightDrawableVisible, true);
				if (!visible) {
					iv_right_drawable.setVisibility(View.GONE);
				}
				break;
			}
		}
	}

	public TopTitle(Context context) {
		super(context);
		init();
	}

	public interface OnLeftDrawableClick {
		void leftClick();
	}

	public interface OnRightDrawableClick {
		void rightClick();
	}

	private OnLeftDrawableClick onLeftDrawableClick;
	private OnRightDrawableClick onRightDrawableClick;

	public void setOnLeftDrawableClick(OnLeftDrawableClick onLeftDrawableClick) {
		this.onLeftDrawableClick = onLeftDrawableClick;
	}

	private OnRightTextClick onRightTextClick;

	public void setOnRightDrawableClick(
			OnRightDrawableClick onRightDrawableClick) {
		this.onRightDrawableClick = onRightDrawableClick;
	}

	public void setOnRightTextClick(OnRightTextClick onRightTextClick) {
		this.onRightTextClick = onRightTextClick;
	}

	public interface OnRightTextClick {
		void rightTextClick();
	}

	private void init() {
		inflater = LayoutInflater.from(getContext());
		inflater.inflate(R.layout.ui_top_title, this);
		iv_left_drawable = (ImageView) this.findViewById(R.id.iv_left_drawable);
		iv_right_drawable = (ImageView) this
				.findViewById(R.id.iv_right_drawable);
		tv_right_text = (TextView) this.findViewById(R.id.tv_right_text);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		iv_left_drawable.setOnClickListener(this);
		tv_right_text.setOnClickListener(this);
		iv_right_drawable.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_left_drawable:
			if (onLeftDrawableClick != null) {
				onLeftDrawableClick.leftClick();
			}
			break;
		case R.id.tv_right_text:
			if (onRightTextClick != null) {
				onRightTextClick.rightTextClick();
			}
			break;
		case R.id.iv_right_drawable:
			if (onRightDrawableClick != null) {
				onRightDrawableClick.rightClick();
			}
			break;
		}
	}

	public void setLeftDrawableVisibily(boolean visible) {
		if (visible) {
			iv_left_drawable.setVisibility(View.VISIBLE);

		} else {
			iv_left_drawable.setVisibility(View.GONE);
		}
	}

	// yx add
	public void setRightTextVisibily(boolean visible) {
		if (visible) {
			tv_right_text.setVisibility(View.VISIBLE);

		} else {
			tv_right_text.setVisibility(View.GONE);
		}
	}

	public void setTitle(String title) {
		tv_title.setText(title);
	}

}
